package eecs.net.xmpp;
//Class: EECS 285
//Project: 4 (Virtual Markerboard)
//Due Date: Monday December 6th, 2010
//

import java.util.Map;
//import java.util.TreeMap;
//import eecs.net.xmpp.DataListener.CallType;
import static eecs.util.Utility.*;

/**
 * This class is primarily to allow for non-blocking calls to
 * incomingSemaphore() and outgoingSemaphore() in the registered listener. This
 * allows the listener to do things in the User Interface concurrently while the
 * packet listener is parsing the incoming packet. Neither has to wait. Note:
 * Currently the call to dataReceived() is simply made directly instead of via
 * this class.
 * 
 * @author Vidal Borromeo
 */
public class DataListenerAsyncCall implements Runnable
{
  DataListener listener = null;
  DataListener.CallType type = DataListener.CallType.UNDEFINED;
  Map<String, Object> map = null;

  // ---------------------------------------------------------------------------

  /**
   * Constructor for an asynchronous call to a DataListener.
   * 
   * @param listener_
   *          The DataListener to be called.
   * @param type_
   *          The DataListener.CallType indicator for the method to call.
   * @param map_
   *          For dataReceived(), the map of properties. Otherwise, just use
   *          'null'.
   * @author Vidal Borromeo
   */
  DataListenerAsyncCall(DataListener listener_, DataListener.CallType type_,
      Map<String, Object> map_)
  {
    listener = listener_;
    type = type_;
    map = map_;
  }

  // ---------------------------------------------------------------------------

  /**
   * The run() method for this Runnable. This  is where the synchronous call is hidden inside this 
   * 
   * 
   * @param listener_
   *          The DataListener to be called.
   * @param type_
   *          The DataListener.CallType indicator for the method to call.
   * @param map_
   *          For dataReceived(), the map of properties. Otherwise, just use
   *          'null'.
   * @author Vidal Borromeo
   */
  @Override
  public void run()
  {
    switch (type)
    {
      case DATA_RECEIVED:
        // debug("Async Call to dataReceived().");
        listener.dataReceived(map);
        break;
      case INCOMING_SEMAPHORE:
        debug("Async Call to incomingSemaphore().");
        listener.incomingSemaphore();
        break;
      case OUTGOING_SEMAPHORE:
        debug("Async Call to outgoingSemaphore().");
        listener.outgoingSemaphore();
        break;
      default:
        debug("Invalid CallType.");
        break;
    }// switch
  }// run

  // ---------------------------------------------------------------------------


}// DataListenerAsyncCall

